/**
 * @author of2470
 *@date 2017/4/1
 *@description
 */
import * as React from "react";
import { Relax } from "iflux2";
import { Button } from "antd";
import { getEnvXSite, openNewWin } from "x-site-util";
import { pageInfoQL } from "../ql";
import ModalTemplate from "./modal-template";
import { getSystemCodeZip } from "../../common/const-value";
import GaUtil from "common-util/ga-util";
import ExpireLock from "expire-lock";

const SystemCodeZip = getSystemCodeZip();
const host = getEnvXSite();

@Relax
export default class LandingPageContent extends React.Component<any, any> {
  static defaultProps = {
    pageInfo: {},
    pageInfoList: pageInfoQL
  };

  modal: any = {};

  render() {
    const { pageInfoList } = this.props;
    let content = pageInfoList
      .map((pageInfo, index) => {
        return (
          <div key={index} className="landing-page">
            <p className="cloud-mall">
              {pageInfo.get("label")}
            </p>
            <p className="the-divider-one" />
            <p className="my-mall-isRelease">
              <span className="cloud-mall-my">
                {pageInfo.get("title")}
              </span>
              <span className="cloud-relese">
                {typeof pageInfo.get("release") !== "undefined"
                  ? pageInfo.get("release") ? "已发布" : "未发布，默认页面"
                  : "未发布，默认页面"}
              </span>
            </p>
            <p className="cloud-mall-url">
              {pageInfo.get("domainUrl")}
            </p>
            <a>
              <Button
                type="primary"
                size="large"
                onClick={this._getEditUrl.bind(
                  this,
                  pageInfo.get("platform"),
                  false
                )}
              >
                编辑
              </Button>
            </a>

            <a
              href="javascript:;"
              target="_blank"
              onClick={this._getDomainUrl.bind(
                this,
                pageInfo.get("platform"),
                pageInfo.get("pageType"),
                pageInfo.get("domainUrl")
              )}
            >
              <Button type="primary" size="large">
                查看
              </Button>
            </a>
            {pageInfo.get("platform") === "app"
              ? null
              : <a
                  href="javascript:;"
                  onClick={this._getEditUrl.bind(
                    this,
                    pageInfo.get("platform"),
                    true
                  )}
                >
                  <Button type="primary" size="large">
                    设置
                  </Button>
                </a>}
            <ModalTemplate
              ref={modal => {
                this.modal = modal;
              }}
            />
          </div>
        );
      })
      .toArray();

    return (
      <div>
        {content}
      </div>
    );
  }

  @ExpireLock()
  _getDomainUrl(platform, pageType, domainUrl) {
    let url = "";
    if (platform == "weixin") {
      url = `${host}/exhibition?platForm=${platform}&pageType=index&sc=${SystemCodeZip}`;
      url = url.indexOf("http://") < 0 ? url.replace("//", "http://") : url;
    } else {
      url = domainUrl;
    }
    openNewWin(url);
  }

  @ExpireLock()
  _getEditUrl(platform, show = false) {
    if (
      this.props.pageInfo &&
      (this.props.pageInfo.get("pc") ||
        this.props.pageInfo.get("weixin") ||
        this.props.pageInfo.get("app"))
    ) {
      let url = `${host}/mysite/${platform}/index?sc=${SystemCodeZip}${show
        ? "#editLeftNavItem2"
        : ""}`;
      openNewWin(url);
    } else {
      this.modal.showModal();
    }
    if (show) {
      GaUtil.addLandingEvent({ platform, eventAction: "点击设置", eventLabel: "" });
    } else {
      GaUtil.addLandingEvent({ platform, eventAction: "编辑页面", eventLabel: "" });
    }
  }
}
